package com.cunfang.util;

import java.lang.reflect.Field;

import org.apache.commons.lang3.reflect.FieldUtils;

/**
 * @author Administrator
 *	反射工具
 */
public class ReflectHelper {
	/**
	 * 获取obj对象fieldName的Field
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getFieldByFieldName(Object obj, String fieldName) {
		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
			}
		}
		return null;
	}

	/**
	 * 获取obj对象fieldName的属性值
	 * @param obj
	 * @param fieldName
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static Object getValueByFieldName(Object obj, String fieldName)
			throws SecurityException, NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		if(field!=null){
			if (field.isAccessible()) {
				value = field.get(obj);
			} else {
				field.setAccessible(true);
				value = field.get(obj);
				field.setAccessible(false);
			}
		}
		return value;
	}

	/**
	 * 设置obj对象fieldName的属性值
	 * @param obj
	 * @param fieldName
	 * @param value
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static void setValueByFieldName(Object obj, String fieldName,
			Object value) throws SecurityException, NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		Field field = obj.getClass().getDeclaredField(fieldName);
		if (field.isAccessible()) {
			field.set(obj, value);
		} else {
			field.setAccessible(true);
			field.set(obj, value);
			field.setAccessible(false);
		}
	}
	
	 public static Object getFieldValue(Object obj , String fieldName ){  
         
	        if(obj == null){  
	            return null ;  
	        }  
	          
	        Field targetField = getTargetField(obj.getClass(), fieldName);  
	          
	        try {  
	            return FieldUtils.readField(targetField, obj, true ) ;  
	        } catch (IllegalAccessException e) {  
	            e.printStackTrace();  
	        }   
	        return null ;  
	    }  
	      
	    public static Field getTargetField(Class<?> targetClass, String fieldName) {  
	        Field field = null;  
	  
	        try {  
	            if (targetClass == null) {  
	                return field;  
	            }  
	  
	            if (Object.class.equals(targetClass)) {  
	                return field;  
	            }  
	  
	            field = FieldUtils.getDeclaredField(targetClass, fieldName, true);  
	            if (field == null) {  
	                field = getTargetField(targetClass.getSuperclass(), fieldName);  
	            }  
	        } catch (Exception e) {  
	        }  
	  
	        return field;  
	    }  
	      
	    public static void setFieldValue(Object obj , String fieldName , Object value ){  
	        if(null == obj){return;}  
	        Field targetField = getTargetField(obj.getClass(), fieldName);    
	        try {  
	             FieldUtils.writeField(targetField, obj, value) ;  
	        } catch (IllegalAccessException e) {  
	            e.printStackTrace();  
	        }   
	    }   
}
